// 1. 引入
import Vue from 'vue'
import Router from 'vue-router'

// 2. 全局注册
Vue.use(Router)

// 3. 静态引入Layout页面组件(主体架子)
import Layout from '@/layout'

// 4. 引入模块路由规则对象
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'

// 5. 静态路由规则数组(里面的所有路由对象都不做权限管理)
export const constantRoutes = [
  // 一级路由 - 登录页面
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  // 一级路由 - 404
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 一级路由 - 首页版块
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      // 二级路由
      {
        path: 'dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        // 元信息对象: 配置一些额外的数据
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  // 一级路由 - 上传excel文件版块
  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'import',
        component: () => import('@/views/import/index')
      }
    ]
  }
  // 当所有的规则都匹配不上, 统一走404(都要放在规则数组的尾部)
  // { path: '*', redirect: '/404', hidden: true }
]

// 6. 动态路由规则数组(里面的所有路由对象后期都要权限管理)
export const asyncRoutes = [
  departmentsRouter, // 组织架构
  settingRouter, // 设置
  employeesRouter, // 员工
  permissionRouter, // 权限
  approvalsRouter, // 审批
  attendancesRouter,
  salarysRouter,
  socialRouter
]

// 7. 调用函数, 创建一个全局的路由对象
const createRouter = () => new Router({
  mode: 'history', // 配置路由模式
  // 在页面切换时, 进入到新的页面时, 都会滚动到页面的顶部 scorllTop = 0
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    ...constantRoutes
    // ...asyncRoutes
  ] // 路由规则对象数组
})
const router = createRouter()

// 8. 重置路由对象
export function resetRouter() {
  const newRouter = createRouter()
  // console.log(router.matcher, '88888')
  router.matcher = newRouter.matcher // reset router
  // console.log(router.matcher, '66666')
}

// 9. 导出路由对象
export default router
